package cn.iocoder.yudao.module.data.controller.admin.dscategory;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.data.controller.admin.dscategory.vo.*;
import cn.iocoder.yudao.module.data.dal.dataobject.dscategory.DsCategoryDO;
import cn.iocoder.yudao.module.data.service.dscategory.DsCategoryService;

/**
 * @author gukun
 */
@Tag(name = "管理后台 - 分类")
@RestController
@RequestMapping("/data/ds-category")
@Validated
public class DsCategoryController {

    @Resource
    private DsCategoryService dsCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建分类")
    public CommonResult<Long> createDsCategory(@Valid @RequestBody DsCategorySaveReqVO createReqVO) {
        return success(dsCategoryService.createDsCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分类")
    public CommonResult<Boolean> updateDsCategory(@Valid @RequestBody DsCategorySaveReqVO updateReqVO) {
        dsCategoryService.updateDsCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('data:ds-category:delete')")
    public CommonResult<Boolean> deleteDsCategory(@RequestParam("id") Long id) {
        dsCategoryService.deleteDsCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DsCategoryRespVO> getDsCategory(@RequestParam("id") Long id) {
        DsCategoryDO dsCategory = dsCategoryService.getDsCategory(id);
        return success(BeanUtils.toBean(dsCategory, DsCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分类分页")
    public CommonResult<PageResult<DsCategoryRespVO>> getDsCategoryPage(@Valid DsCategoryPageReqVO pageReqVO) {
        PageResult<DsCategoryDO> pageResult = dsCategoryService.getDsCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DsCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分类 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDsCategoryExcel(@Valid DsCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DsCategoryDO> list = dsCategoryService.getDsCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分类.xls", "数据", DsCategoryRespVO.class,
                        BeanUtils.toBean(list, DsCategoryRespVO.class));
    }


    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public CommonResult<List<DsCategoryRespVO>>  getCategoryList(DsCategoryListReqVO reqVO) {
        List<DsCategoryDO> list = dsCategoryService.getDsList(reqVO);
        return success(BeanUtils.toBean(list, DsCategoryRespVO.class));
    }

    @GetMapping("/table-list")
    @Operation(summary = "获取表格列表")
    public CommonResult<PageResult<DsTableRespVO>>  getTableList(DsTableListPageReqVO reqVO) {
        List<DsTableRespVO> list = dsCategoryService.getTableList(reqVO);
        if (list == null || list.isEmpty()) {
            return success(new PageResult<>());
        }
        PageResult<DsTableRespVO> pageResult = new PageResult<>();
        Integer pageSize = reqVO.getPageSize();
        Integer pageNum = reqVO.getPageNo();
        int toIndex = Math.min(pageNum + pageSize, list.size());
        pageResult.setList(list.subList(pageNum, toIndex));
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }


    @GetMapping("/getTableListByDsId")
    @Operation(summary = "获取表格列表")
    public CommonResult<List<DsTableRespVO>>  getTableListByDsId(@RequestParam("dsId") Long dsId) {
        List<DsTableRespVO> tableList = dsCategoryService.getTableList(dsId);
        if (tableList == null || tableList.isEmpty()) {
            return success(new ArrayList<>());
        }
        return success(tableList);
    }




}